package main

import (
	"github.com/gin-gonic/gin"
	"net/http"
	"time"
)

func GetTime(c *gin.Context) {

	type resData struct {
		Status  int         `json:"status"`
		Message interface{} `json:"message"`
		Time    interface{} `json:"time"`
	}
	c.JSON(http.StatusOK, resData{Status: http.StatusOK, Message: "Get time succeed.", Time:time.Now().Unix()})
}

func FaceSearch(c *gin.Context) {
	var (
		resp *http.Response
		err error
	)

	resp, err = http.Post("https://aip.baidubce.com/rest/2.0/face/v3/search?" +
		"access_token=24.c9b9fc0b044a9022de162658ff88f9c6.2592000.1637848107.282335-25056569",
		"application/json",
		c.Request.Body)
	if err != nil || resp.StatusCode != http.StatusOK {
		c.Status(http.StatusServiceUnavailable)
		return
	}
	reader := resp.Body
	//a, _ := ioutil.ReadAll(reader)
	//fmt.Println(string(a))
	contentLength := resp.ContentLength
	contentType := resp.Header.Get("Content-Type")
	extraHeaders := map[string]string{
		"Content-Disposition": "",
	}
	c.DataFromReader(http.StatusOK, contentLength, contentType, reader, extraHeaders)
}

func GetWeather(c *gin.Context) {
	var (
		resp *http.Response
		err error
	)

	resp, err = http.Get("https://wxc.gd121.cn/gdecloud/servlet/servletcityweatherall4?" +
		"DISTRICTCODE=440112&LNG=113.48&LAT=23.18&FROM=binfen")
	if err != nil || resp.StatusCode != http.StatusOK {
		c.Status(http.StatusServiceUnavailable)
		return
	}

	reader := resp.Body
	//a, _ := ioutil.ReadAll(reader)
	//fmt.Println(string(a))
	contentLength := resp.ContentLength
	contentType := resp.Header.Get("Content-Type")
	extraHeaders := map[string]string{
		"Content-Disposition": "",
	}
	c.DataFromReader(http.StatusOK, contentLength, contentType, reader, extraHeaders)
}

func main() {
	router := gin.Default()
	router.StaticFS("/video", http.Dir("video"))
	// https://www.cnblogs.com/johnsonshu/p/7394508.html
	// https://blog.csdn.net/shichimiyasatone/article/details/86596631?utm_medium=distribute.pc_aggpage_search_result.none-task-blog-2~aggregatepage~first_rank_ecpm_v1~rank_aggregation-1-86596631.pc_agg_rank_aggregation&utm_term=ffmpeg%E7%AD%89%E6%AF%94%E4%BE%8B%E8%B0%83%E6%95%B4&spm=1000.2123.3001.4430

	router.GET("/time", GetTime)
	router.POST("/face_search", FaceSearch)
	router.GET("/weather", GetWeather)

	//listen
	_ = router.Run(":8080")
}
